<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/2/12
 * Time: 9:33 AM
 * To change this template use File | Settings | File Templates.
 */
class Db_Locale_ProjectCountriesDeliveryMethods extends Common {
	protected static $tableName = TAB_PROJECT_COUNTRIES_DELIVERY_METHODS;
	protected static $dbFields = array(
		'id',
		'pcdm_project_id',
		'pcdm_delivery_method_id',
		'pcdm_country_id',
		'pcdm_price',
		'pcdm_show_on_page',
		'pcdm_position',
		'pcdm_description',
		'pcdm_available_for_sample',
		'pcdm_is_default',
		'pcdm_days_restrictions',
		'deleted'
	);
	/**
	 * @var Db_Locale_ProjectCountriesDeliveryMethods
	 */
	public $objectArray;

	public static $positionField = 'pcdm_position';

	public $id;
	public $pcdm_project_id;
	public $pcdm_delivery_method_id;
	public $pcdm_country_id;
	public $pcdm_price;
	public $pcdm_show_on_page;
	public $pcdm_position;
	public $pcdm_description;
	public $pcdm_available_for_sample;
	public $pcdm_is_default;
	public $pcdm_days_restrictions;
	public $deleted;

	function __construct($value = null, $fieldName = null) {
		parent::__construct($value, $fieldName);
	}

	public static function getCountryDeliveryMethodsByProjectId($projectId) {
		$sql = "SELECT `pcdm`.*, `dm`.`dm_name`, `pcdm`.`id` AS `pcdm_id`, `ct`.`c_name` FROM ?f AS `pcdm`
				INNER JOIN ?f AS `dm` ON `pcdm`.`pcdm_delivery_method_id` = `dm`.`id`
				INNER JOIN ?f AS `ct` ON `ct`.`id` = `pcdm`.`pcdm_country_id`
				WHERE `dm`.`deleted` = 0 AND `pcdm`.`deleted` = 0 AND `ct`.`deleted` = 0 AND `pcdm`.`pcdm_project_id` = ?
				ORDER BY `pcdm`.`pcdm_position` ASC";
		return self::find($sql, self::$tableName, TAB_DELIVERY_METHODS, TAB_COUNTRIES, $projectId);
	}

	public static function getProjectCountriesDeliveryMethods() {
		$sql = "SELECT `pcdm`.*, `ct`.`id` AS `country_id`, `pcdm`.`pcdm_project_id`, `ct`.`c_name`, `dm`.`dm_name`
				FROM ?f AS `pcdm`
				INNER JOIN ?f AS `ct` ON `pcdm`.`pcdm_country_id` = `ct`.`id`
				INNER JOIN ?f AS `dm` ON `pcdm`.`pcdm_delivery_method_id` = `dm`.`id`
				WHERE `pcdm`.`deleted` = 0 AND `ct`.`deleted` = 0 AND `dm`.`deleted` = 0
				GROUP BY `ct`.`id`, `dm`.`id`;";

		$result = self::$DB->q($sql, self::$tableName, TAB_COUNTRIES, TAB_DELIVERY_METHODS, TAB_PROJECTS);
		$dataReturn = array();
		while ($data = $result->fetch_object()) {
			$dataReturn[$data->pcdm_project_id]['country'] = $data->c_name;
			$dataReturn[$data->pcdm_project_id]['methods'][$data->pcdm_delivery_method_id] = array(
				'countryId' => $data->country_id,
				'deliverymethodid' => $data->pcdm_delivery_method_id,
				'content' => $data->dm_name,
				'sample_active' => $data->pcdm_available_for_sample
			);
		}
		return $dataReturn;
	}

	public static function getCountriesDeliveryMethodsByProjectId ($projectId) {
		$sql = "SELECT `c`.`c_name`, `c`.`id`, `pcdm`.`pcdm_is_default`	  FROM ?f pcdm
		        LEFT JOIN ?f c ON `pcdm`.`pcdm_country_id` = `c`.`id` AND `c`.`deleted` = 0
				WHERE `pcdm`.`pcdm_project_id` = ?  AND `pcdm`.`deleted` = 0
				GROUP BY `c`.`id` ORDER BY  `pcdm`.`pcdm_is_default` DESC, `c`.`id`";

		$deliveryCountries = self::$DB->q($sql, self::$tableName, TAB_COUNTRIES, $projectId);
		$result = array();
		while($data = $deliveryCountries->fetch_assoc()) {
			$result[$data['id']] = $data['c_name'];
		}
		return $result;
	}

	public static function getDeliveryMethodsByProjectIdAndCountryId ($projectId, $countryId) {
		if (!$projectId || !$countryId) {
			return false;
		}
		$sql = "SELECT `pcdm`.*, `dm`.`dm_name` FROM ?f pcdm
                LEFT JOIN ?f dm ON `dm`.`id` = `pcdm`.`pcdm_delivery_method_id` AND `dm`.`deleted` = 0
                WHERE `pcdm`.`pcdm_project_id` = ? AND `pcdm`.`pcdm_country_id` = ?  AND `pcdm`.`deleted` = 0 ";
		$result = self::$DB->q(true,$sql, self::$tableName, TAB_DELIVERY_METHODS, $projectId, $countryId);
		$dataReturn = array();
		while ($deliveries = $result->fetch_assoc()) {
			$dataReturn[$deliveries['id']] = $deliveries;
			$dataReturn[$deliveries['id']]['delivery_date'] = Db_Locale_ProjectCountriesDeliveryMethodsDays::deliveryDate($deliveries['id'], $deliveries['pcdm_days_restrictions']);
		}
		return $dataReturn;
	}
}
